import { Adsdk } from "../../myScript/Adsdk";
import { GlobalTmpData } from "../Config/GlobalTmpData";
import EventManager from "../Managers/EventManager";
import { EventTypes } from "../Managers/EventTypes";
import { AudioSystem } from "../SystemAudio/AudioSystem";
import { clog } from "../Tools/ColorLog";


export default class SDK {
    protected api = null;
    // protected adConfig: uniSdk.AdConfig = null;

    public async init(callback?: Function, target?: any) {
        this.onEvents();
        //第一步：先配置好广告单元ID
        // this.adConfig = new uniSdk.AdConfig();
        // this.adConfig.adTableName = "RenLeiBieTao";

        this.setAdCfg();
        await this.initSdk(callback, target);
    }

    // 监听事件
    protected onEvents() {
        // EventManager.on(EventTypes.SDKEvents.ShowBanner, this.showBanner, this);
        // EventManager.on(EventTypes.SDKEvents.HideBanner, this.hideBanner, this);
        EventManager.on(EventTypes.SDKEvents.ShowVideo, this.showRewardedVideo, this);
        // EventManager.on(EventTypes.SDKEvents.ShowInsertAd, this.showInterstitial, this);
        // EventManager.on(EventTypes.SDKEvents.ShowCustomAd, this.showCustomAd, this);
        // EventManager.on(EventTypes.SDKEvents.HideCustomAd, this.hideCustomAd, this);
        // EventManager.on(EventTypes.SDKEvents.Share, this.share, this);
        EventManager.on(EventTypes.SDKEvents.ExitApp, this.onExitApp, this);
    }

    /**
    * 初始化SDK，操作所在SDK接口之前必须对SDK进行初始化 
    */
    protected setAdCfg() {


    }

    private async initSdk(callback?: Function, target?: any) {

        // let api = window['wx'];
        // if (api) {
        //     //微信分享
        //     api.showShareMenu({
        //         withShareTicket: true,
        //         menus: ['shareAppMessage', 'shareTimeline']
        //     })
        // }
        // await wxsdk.InitSdk(10146, "0.0.1") //传入运营指定的后台id和版本       
        // if (callback) callback.call(target);
    }
    //#region ----------------功能---------
    //提示消息
    protected showMessage(msg: string) {
        EventManager.emit(EventTypes.GameEvents.ShowTips, msg);
    }

    //#endregion

    // #region -----------------事件---------------
    /**
       * 显示 Banner 广告
       */
    protected showBanner() {
        console.log("showBanner");
        // uniSdk.showBanner();
    }

    /**
     * 手动关闭 Banner 广告
     */
    protected hideBanner() {
        console.log("hideBanner");
        // uniSdk.hideBanner();
    }

    /**
     * 拉起激励视频广告
     */
    protected async showRewardedVideo(cb: Function |
    { success: Function, fail: Function, cancel: Function, autoAudio?: boolean, autoResume?: boolean }) {
      
        // let success = null;
        // let fail = null;
        // let cancel = null;
        // let autoAudio = true;
        // let autoResume = true;

        // if (typeof cb === 'object') {
        //     success = cb.success;
        //     fail = cb.fail;
        //     cancel = cb.cancel;
        //     if (cb.autoAudio != undefined) {
        //         autoAudio = cb.autoAudio;
        //     }
        //     if (cb.autoResume != undefined) {
        //         autoResume = cb.autoResume;
        //     }
        // } else {
        //     success = cb;
        // }
        // //暂停游戏
        // EventManager.emit(EventTypes.GameEvents.GamePause);
        // AudioSystem.pauseBGM();
        // //
        // console.warn('观看视频中12');
        //激励视频使用方法
        // var result = await wxsdk.GetAdById("Video");
        // if (result == "Success") {
        //     console.log("激励视频已观看完毕 ");

        //     if (autoResume) {
        //         EventManager.emit(EventTypes.GameEvents.GameResume);
        //     }
        //     //恢复游戏
        //     setTimeout(() => {
        //         autoAudio && AudioSystem.resumeBGM();
        //     }, 0);
        //     success && success();
        // };
        // if (result == "Fail") {
        //     if (autoResume) {
        //         EventManager.emit(EventTypes.GameEvents.GameResume);
        //     }
        //     console.log("激励视频未观看完毕 ");
        //     EventManager.emit(EventTypes.GameEvents.ShowTips, '激励视频未观看完毕');
        //     setTimeout(() => {
        //         autoAudio && AudioSystem.resumeBGM();
        //     }, 0);
        //     cancel && cancel();
        // };
        // if (result == "Error") {
        //     console.log("激励视频加载失败 ");
        //     EventManager.emit(EventTypes.GameEvents.ShowTips, '激励视频加载失败');
        //     if (autoResume) {
        //         EventManager.emit(EventTypes.GameEvents.GameResume);
        //     }
        //     setTimeout(() => {
        //         autoAudio && AudioSystem.resumeBGM();
        //     }, 0);
        //     fail && fail();
        // };

        let success = null;
        let fail = null;
        let autoAudio = true;
        let autoResume = true;

        if (typeof cb === 'object') {
            success = cb.success;
            fail = cb.fail;
            if (cb.autoAudio != undefined) {
                autoAudio = cb.autoAudio;
            }
            if (cb.autoResume != undefined) {
                autoResume = cb.autoResume;
            }
        } else {
            success = cb;
        }
        //暂停游戏
        EventManager.emit(EventTypes.GameEvents.GamePause);
        AudioSystem.pauseBGM();
        //
        console.warn('观看视频中12');
        //激励视频使用方法
        Adsdk.instance.showVideo(()=>{
            console.log("激励视频已观看完毕 ");

            if (autoResume) {
                EventManager.emit(EventTypes.GameEvents.GameResume);
            }
            //恢复游戏
            setTimeout(() => {
                autoAudio && AudioSystem.resumeBGM();
            }, 0);
            success && success();
        },()=>{
            if (autoResume) {
                EventManager.emit(EventTypes.GameEvents.GameResume);
            }
            setTimeout(() => {
                autoAudio && AudioSystem.resumeBGM();
            }, 0);
            fail && fail();
        })
       
    }

    /**
       * 插屏广告
       */
    protected showInterstitial(d?: { success?: Function, fail?: Function, close?: Function }) {
        console.log("showInterstitial");
        //触摸屏蔽
        // EventManager.emit(EventTypes.GameEvents.SetTouchMaskEnable,)
        uniSdk.showInterstitial((showed: boolean) => {
            if (showed) {
                console.log("插屏广告显示成功");
                d && d.success && d.success();
            }
            else {
                console.log("插屏广告显示失败， 这里处理失败的逻辑");
                // 插屏广告显示失败， 尝试原生模板
                // uniSdk.showCustomAd(0);
                d && d.fail && d.fail();
            }
        }, () => {
            console.log("插屏广告被用户关闭， 这里处理广告关闭后的逻辑");
            d && d.close && d.close();
        }, this);

    }


    /**
     * 显示原生模板广告
     */
    protected showCustomAd(adIndex: number) {
        console.log("showCustomAd");
        // 第一个参数 0 表示：使用原生模板广告配置中的第一个广告ID
        uniSdk.showCustomAd(adIndex, (showed: boolean) => {

            if (showed) {
                console.log("原生模板广告显示成功");
            }
            else {
                console.log("原生模板广告显示失败 ");
                // 原生模板广告失败，尝试用插屏广告顶上
                uniSdk.showInterstitial();
            }

        }, () => {
            console.log("原生模板广告被用户关闭 ");
        }, this);
    }

    /**
     * 手动关闭原生模板广告
     */
    protected hideCustomAd(adIndex: number) {
        console.log("hideCustomAd");

        // 第一个参数 0 表示：关闭原生模板广告配置中的第一个广告
        uniSdk.hideCustomAd(adIndex);
    }

    /**
     * 自定义分享
     */
    protected share() {
        console.log("share");
        uniSdk.share();
    }
    // #endregion

    //退出游戏
    protected onExitApp() {

    }
}


/**
 * 重写SDK内的getCustomAdStyle方法重新定义原生模板广告的显示样式
 */
uniSdk.getCustomAdStyle = function (index: number) {
    let sysinfo: SystemInfo = uniSdk.getSystemInfo();
    //实际分辨率宽高
    console.log('sysinfo:', sysinfo.windowHeight, sysinfo.windowWidth);
    let style: any = null;

    if (uniSdk.Global.isWxgame) {
        //
        let width = 320;
        let height = 400;
        let top = 0;
        let left = 0;

        switch (index) {
            case 0:     //5x5 
                width = 320;
                height = 400;
                top = ((sysinfo.windowHeight - height) * 0.5) - 40;
                left = (sysinfo.windowWidth - width) * 0.5;
                break;
            // case 1: //homeLeft
            //     width = 110;
            //     height = 110;
            //     top = (sysinfo.windowHeight - height) * 0.5 - 50;
            //     left = 10;
            //     break;
            // case 2: //homeRight
            //     width = 110;
            //     height = 110;
            //     top = (sysinfo.windowHeight - height) * 0.5 - 50;
            //     left = sysinfo.windowWidth - width * 0.5 - 10;
            //     break;
            case 1: //level_right_1
                width = 110;
                height = 110;
                top = (sysinfo.windowHeight - height) * 0.5 - 150;
                left = sysinfo.windowWidth - width * 0.5 - 10;
                break;
            case 2: //level_left_2
                width = 110;
                height = 110;
                top = (sysinfo.windowHeight - height) * 0.5 - 150;
                left = 10;
                break;
        }

        style = {
            top: top,
            left: left,
            fixed: false,
            width: width
        }
    }
    return style;
}